package com.kfm.bootdemo.service;

import com.kfm.bootdemo.dao.StudentDao;
import com.kfm.bootdemo.model.Page;
import com.kfm.bootdemo.model.Student;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentService {

    private StudentDao dao = new StudentDao();

    public List<Student> findAll() {
        try {
            return dao.selectAll();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public int editStudent(Integer id, String name, String birth, String gender) {
        if (id == null) {
            return -1;
        }
        try {
            return dao.editStudent(id, name, birth, gender);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public Page<Student > findPage(Integer pageNum, Integer pageSize, Student student) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        try {
            List<Student> students = dao.selectPage(pageNum, pageSize, student);
            // 当前页 每页数量 查询出来的数据
            Page<Student> page = new Page<>(pageNum, pageSize, findTotal(student), students);
            return page;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public long findTotal(Student student) {
        try {
            return dao.selectTotal(student);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public List<Student> findBYCondition(Student student) {
        try {
            return dao.selectByStudent(student);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
}
